package com.ophiux.cs.api.consultBase.impl;

import com.google.gson.reflect.TypeToken;
import com.ophiux.cs.api.consultBase.port.IHospitalApiPort;
import com.ophiux.cs.application.consultBase.dto.*;
import com.ophiux.cs.application.consultBase.service.IAddHospitalAppService;
import com.ophiux.cs.application.consultBase.service.IDeleteHospitalAppService;
import com.ophiux.cs.application.consultBase.service.IGetHospitalAppService;
import com.ophiux.cs.application.consultBase.service.IUpdateHospitalAppService;
import com.ophiux.cs.application.core.AppResultCode;
import com.ophiux.cs.application.core.Request;
import com.ophiux.cs.application.core.Result;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;

@Service
@Slf4j
public class HospitalApiService implements IHospitalApiPort {
	@Autowired
    IAddHospitalAppService addHospitalAppService;
    @Autowired
    IGetHospitalAppService getHospitalAppService;
    @Autowired
    IUpdateHospitalAppService updateHospitalAppService;
    @Autowired
    IDeleteHospitalAppService deleteHospitalAppService;

    @Override
    public String getHospital(String data) {
        log.info("getHospital接口,请求参数："+data);
        Request<GetHospitalRequest> request = null;
        Result<GetHospitalResponse> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<GetHospitalRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("getHospital接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        GetHospitalRequest dto =request.getPARAM();
        result = getHospitalAppService.getHospital(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("getHospital接口,返回参数："+returnMessage);
        return returnMessage;
    }

    @Override
	public String createHospital(String data) {
		log.info("createHospital接口,请求参数："+data);
        Request<AddHospitalRequest> request = null;
		Result<AddHospitalResponse> result = null;
        try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				@Override
				public Type getObtainType() {
					return new TypeToken<Request<AddHospitalRequest>>() {
					}.getType();
				}
			});
		} catch (Exception e) {
			log.info("createHospital接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}

		AddHospitalRequest dto =request.getPARAM();
		result = addHospitalAppService.createHospital(dto);

		String returnMessage = JsonUtils.toJson(result);
		log.info("createHospital接口,返回参数："+returnMessage);
		return returnMessage;
	}

    @Override
    public String updateHospital(String data) {
        log.info("updateHospital接口,请求参数："+data);
        Request<UpdateHospitalRequest> request = null;
        Result<UpdateHospitalResponse> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<UpdateHospitalRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("updateHospital接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        UpdateHospitalRequest dto =request.getPARAM();
        result = updateHospitalAppService.updateHospital(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("updateHospital接口,返回参数："+returnMessage);
        return returnMessage;
    }

    @Override
    public String deleteHospital(String data) {
        log.info("deleteHospital接口,请求参数："+data);
        Request<DeleteHospitalRequest> request = null;
        Result<DeleteHospitalResponse> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<DeleteHospitalRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("deleteHospital接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        DeleteHospitalRequest dto =request.getPARAM();
        result = deleteHospitalAppService.deleteHospital(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("deleteHospital接口,返回参数："+returnMessage);
        return returnMessage;
    }
}
